public class ProductExt { 

	private final Product2 product;
	private AddItem__c itemListing;
	private List<Category__c> categories;
	private List<Category__c> childCategories;
	private String categoryId;
	private Map<String, PricebookEntry> pbes;
	private List<PricebookEntry> pbesList;
	private String priceBookEntryId;
	private String errorMessage;
	
	public String getCategoryId() { return categoryId; }
	public void setCategoryId(String val) { categoryId = val; }
	
	public String getPriceBookEntryId() { return priceBookEntryId; }
	public void setPriceBookEntryId(String val) { priceBookEntryId = val; }

	public List<Category__c> getChildCategories() { return childCategories; }
	public List<Category__c> getCategories() { 
		if (categories == null) {
			fetchCategories();
		} else if (categoryId != null) {
			fetchCategories();
		}
		return categories; 
	}
	
    public ProductExt(ApexPages.StandardController controller) {
		product = (Product2)controller.getRecord();
		fetchCategories();
		itemListing = new AddItem__c();
    } 

	public List<PricebookEntry> getPricebookEntries() {
		if (pbes == null) {
			pbesList = [Select UseStandardPrice, UnitPrice, ProductCode, Product2Id, Pricebook2Id, 
			Name, IsActive, Id, Pricebook2.Id, PriceBook2.Name From PricebookEntry 
				Where Product2Id = :product.Id and IsActive =  true and Pricebook2.IsActive = true];
			pbes = new Map<String, PricebookEntry>(pbesList);
		}
		return pbesList;
	}
	
	public List<Category__c> fetchCategories() {
		if (categories == null) {
			System.debug('categories is null');
			categories = [Select Name, Id, LeafCategory__c, CategoryParentID__c, CategoryLevel__c, CategoryID__c, BestOfferEnabled__c, AutoPayEnabled__c From Category__c Where Category__c = null];
		} else {
			categories = [Select Name, Id, LeafCategory__C, CategoryParentID__c, CategoryLevel__c, CategoryID__c, BestOfferEnabled__c, AutoPayEnabled__c From Category__c Where Category__c = :categoryId];
		}
		return categories;
	} 
	
	public PageReference getCategoryItems() {
		if (categoryId == null) {
			if ([Select Count() From Category__c] == 0) {
				A_Test.doGetCategories();
			} 
			fetchCategories();
		} else {
			if ([Select Count() From Category__c Where Category__c = :categoryId] == 0) {
				A_Test.doGetCategoryChildren(categoryId);
			}
			fetchCategories();
		}
		return null;
	}
	
	public PageReference getCategoryChildren() {
		if ([Select Count() From Category__c Where Category__c = :categoryId] == 0) {
			A_Test.doGetCategoryChildren(categoryId);
		}
		categories = [Select Name, Id, CategoryParentID__c, CategoryLevel__c, CategoryID__c, BestOfferEnabled__c, AutoPayEnabled__c From Category__c Where Category__c = :categoryId];
		return null;
	}
	
	public AddItem__c getItemListing() {
		if (itemListing == null) {
			itemListing = new AddItem__c();
		}
		return itemListing;
	}
	 
	public PageReference setDefaults() {
		if (!pbes.containsKey(priceBookEntryId)) {
			itemListing.Title__c = null;
			itemListing.StartPrice__c = null;
			itemListing.Description__c = null;
		} else {
			itemListing.Title__c = product.Name;
			itemListing.StartPrice__c = pbes.get(priceBookEntryId).UnitPrice / 2;
			itemListing.Description__c = product.Description;
		}
		return null;
	} 
	public PageReference createAuction() {
		AddItemResponseType.AddItemResponseType_e resp = A_Test.doCreateAuction(itemListing);
		return null;
	}
}